package sysprojectmg.gui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import sysprojectmg.seguridad.be.BeanUsuario;
import sysprojectmg.seguridad.biz.ControladorUsuario;
import sysprojectmg.utiles.LE;
/**
 *
 * @author Metis Gaia 2010-02
 */
public class FrmVentanaLogin extends javax.swing.JFrame {

    ControladorUsuario controladorUsuario = new ControladorUsuario();
    ArrayList<BeanUsuario> listaUsuarios = null;
    static String UsuarioSeleccionado = "";

    void setUsuarioSeleccionado(String Usuario) {
        UsuarioSeleccionado = Usuario;
    }

    String getUsuarioSeleccionado() {
        return UsuarioSeleccionado;
    }

    public FrmVentanaLogin() throws IOException {
        try {
            listaUsuarios = controladorUsuario.listarUsuarios();
        } catch (Exception ex) {
            Logger.getLogger(FrmVentanaLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbxUsuario = new javax.swing.JComboBox();
        lblLogin = new javax.swing.JLabel();
        lblUsuario = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        btnIngresar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Metis Gaia");
        setResizable(false);

        try {
            ArrayList<String> lista = new ArrayList<String>();
            lista.add("[SELECCIONE]");
            for (BeanUsuario x : listaUsuarios){
                lista.add(x.getUsuario());
            }

            String [] listaUsuarios = new String[lista.size()];
            lista.toArray(listaUsuarios);
            cbxUsuario.setModel(new DefaultComboBoxModel(listaUsuarios) );
        } catch (Exception ex) {
            Logger.getLogger(FrmVentanaLogin.class.getName()).log(Level.SEVERE, null, ex);
        }

        lblLogin.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblLogin.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblLogin.setText("LOGIN");

        lblUsuario.setText("Usuario:");

        lblPassword.setText("Password:");

        txtPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPasswordActionPerformed(evt);
            }
        });

        btnIngresar.setText("INGRESAR");
        btnIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresarActionPerformed(evt);
            }
        });

        btnCancelar.setText("CANCELAR");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblLogin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(lblUsuario)
                                        .addGap(14, 14, 14)
                                        .addComponent(cbxUsuario, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(lblPassword)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addComponent(btnIngresar)
                        .addGap(39, 39, 39)
                        .addComponent(btnCancelar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(lblLogin)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsuario)
                    .addComponent(cbxUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPassword)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnIngresar)
                    .addComponent(btnCancelar))
                .addGap(57, 57, 57))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresarActionPerformed

        // TODO add your handling code here:
        boolean valido = false;

        if (cbxUsuario.getSelectedIndex() != 0) {
            try {
                valido = controladorUsuario.validarUsuario(listaUsuarios, cbxUsuario.getSelectedItem().toString(), String.valueOf(txtPassword.getPassword()));
                if (valido == true) {
                    setUsuarioSeleccionado(cbxUsuario.getSelectedItem().toString());
                    FrmVentanaPrincipal frmVPrincipal = new FrmVentanaPrincipal();
                    frmVPrincipal.setIdUsuario(controladorUsuario.obtenerId(listaUsuarios, cbxUsuario.getSelectedItem().toString()));
                    frmVPrincipal.setTitle("SysProjectMG");
                    frmVPrincipal.setVisible(true);
                    this.setVisible(false);
//                    EleccionProyecto objEleccionProyecto = new EleccionProyecto();
//                    objEleccionProyecto.setIdusuario(controladorUsuario.obtenerId(listaBean, cbxUsuario.getSelectedItem().toString()));
//                    objEleccionProyecto.setVisible(true);

                } else {
                    LE.mostrarError("Contraseña inválida");
                }
            } catch (Exception ex) {
                Logger.getLogger(FrmVentanaLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            LE.mostrarError("Seleccione un usuario por favor");
        }


    }//GEN-LAST:event_btnIngresarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPasswordActionPerformed
        // TODO add your handling code here:
        btnIngresar.doClick();
    }//GEN-LAST:event_txtPasswordActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new FrmVentanaLogin().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(FrmVentanaLogin.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnIngresar;
    private javax.swing.JComboBox cbxUsuario;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JPasswordField txtPassword;
    // End of variables declaration//GEN-END:variables
}
